package admin

import (
	"VirtualCamera/common"
	"VirtualCamera/controllers"
	"VirtualCamera/models"
	"VirtualCamera/response"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type RegistController struct {
	controllers.BaseController
}

func (con RegistController) Regist(c *gin.Context) {

	db := common.GetDB()

	var requestUser = models.User{}
	err := c.Bind(&requestUser)
	fmt.Println(requestUser)
    if err != nil {
		response.Response(c, http.StatusOK, 0, nil, "数据接收错误")
	}
	username := requestUser.Name
	fmt.Println("username",username)
	//判断用户名是否存在,不存在才可以注册
	var user models.User
	db.Where("name = ? ", username).Find(&user)
	if user.ID != 0 {
		response.Response(c, http.StatusOK, 400, nil, "用户名已存在")
		return
	}

	//用户名不存在 可以增加进入数据库
	user.Name = requestUser.Name
	user.Password = requestUser.Password
	user.Email = requestUser.Email
	user.Telephone = requestUser.Telephone

	result := db.Create(&user)
	if result.Error != nil {
		fmt.Println(result.Error)
		response.Response(c, http.StatusOK, 400, nil, result.Error.Error())
		return
	}

	response.Success(c, nil, "注册成功")

}

func (con RegistController) GetUserInfo(c *gin.Context) {
	db := common.GetDB()

	username := c.Query("Name")
	fmt.Println(username)
	// var requestUser = models.UserInfo{}
	// c.Bind(&requestUser)

	// username := requestUser.Name
	// fmt.Println(requestUser)
	fmt.Println(username)
	//判断用户名是否存在,不存在则失败
	var user models.User
	db.Where("name = ? ", username).Find(&user)
	if user.ID == 0 {
		response.Response(c, http.StatusOK, 400, nil, "用户不存在")
		return
	}

	ret := gin.H{
		"username":  user.Name,
		"email":     user.Email,
		"telephone": user.Telephone,
	}
	response.Success(c, ret, "查询成功")
}
